#include "Monitor.h"

#include "Tools.h"

uint8 show[8];
uint8 offset;
int8  _dotpot;

/*数码管code*/
static __code uint8 code[16] = {0x3f,  // 0
                                0x06,  // 1
                                0x5b,  // 2
                                0x4f,  // 3
                                0x66,  // 4
                                0x6d,  // 5
                                0x7d,  // 6
                                0x07,  // 7
                                0x7f,  // 8
                                0x6f,  // 9
                                0x40,  // -
                                0x00,  //
                                0x79,  // E
                                0x00}; //

/*
void
monitor_init (void)
{
        n_buffer_obj = N_BUFFER (monitor_obj);
        n_buffer_init ();
        for (i = 0; i < 8; ++i)
                show[i] = 0x0d;
        offset = 11;
}
*/

/**
 * 推送缓存到数码管进行显示
 */
void
monitor_push (void)
{
        uint8 _c;
        for (i = 0; i < 8; ++i) {
                P2_2 = ((i & 0x1) != 0);
                P2_3 = ((i & 0x2) != 0);
                P2_4 = ((i & 0x4) != 0);
                _c   = show[i];
                P0_0 = ((_c & 0x01) != 0);
                P0_1 = ((_c & 0x02) != 0);
                P0_2 = ((_c & 0x04) != 0);
                P0_3 = ((_c & 0x08) != 0);
                P0_4 = ((_c & 0x10) != 0);
                P0_5 = ((_c & 0x20) != 0);
                P0_6 = ((_c & 0x40) != 0);
                P0_7 = ((_c & 0x80) != 0);
                delay (10);
                P0_0 = 0;
                P0_1 = 0;
                P0_2 = 0;
                P0_3 = 0;
                P0_4 = 0;
                P0_5 = 0;
                P0_6 = 0;
                P0_7 = 0;
        }
}

static void
monitor_load_offset (void)
{
        if ((dotpot & 0xf0) == 0xb0) {
                offset = 11;
                goto exit;
        }

        for (i = 11; i >= 7; --i) {
                if (n_buffer_getnum_at (i) != 0xd) {
                        offset = i;
                        goto exit;
                }
        }
        offset = 7;

exit:
        _dotpot = dotpot;
}

void
monitor_refresh_cache (uint8 is_reload_offset)
{
        uint8 tmp, _offset;

        if (is_reload_offset)
                monitor_load_offset ();
        _offset = offset;

        for (i = 8; i > 0; --i) {
                tmp         = n_buffer_getnum_at (_offset--);
                show[i - 1] = code[tmp];
        }

        if ((dotpot & 0xf0) != 0xb0)
                return;

        if (((scibuf[1] & 0xf0) == 0xc0) || ((scibuf[1] & 0xf0) == 0xb0)) {
                for (i = 4; i > 0; --i) {
                        if (i % 2)
                                tmp = scibuf[(i - 1) / 2] & 0x0f;
                        else
                                tmp = scibuf[(i - 1) / 2] >> 4;
                        show[i - 1] = code[tmp];
                }
        }
        tmp = 8 - (_dotpot & 0x0f);
        if (tmp < 8)
                show[tmp] ^= 0x80;
}
